﻿
var dialog = new Dialog();

//对话框
function Dialog() {
    var dialogHtml = "<div class='box_bg'><center><div class='box'><div class='title'><span>{title}</span><div id = 'boxCancel'class='cancel'><i class='fa fa-close'></i></div></div>{type}<input id='boxOk' type='button' value='确认' /></div></center></div>";
    var inputBoxHtml = "<div class='div_ipt'><span>{msg}</span><input id = 'box_input' type='text' /></div>"
    var confirmBoxHtml = "<div class='div_confirm'><img src='img/waring.png' /><span>{msg}</span></div>";

    //输入窗口
    this.inputBox = function (title, msg, ok, cancel) {
        $(".box_bg").remove();

        var html = dialogHtml.replace(/{title}/g, title).replace(/{type}/g, inputBoxHtml.replace(/{msg}/g, msg));
        $("body").append(html);

        //确认时间绑定
        $("#boxOk").bind("click", function () {
            if (ok != null) {
                var val = $("#box_input").val();
                ok(val);
            }

            $(".box_bg").remove();
        });

        //取消事件绑定
        $("#boxCancel").bind("click", function () {
            if (cancel != null) {
                cancel();
            }
            $(".box_bg").remove();
        });


        $('#box_input').focus();
    }

    //确认窗口
    this.confirm = function (title, msg, ok, cancel) {
        $(".box_bg").remove();

        var html = dialogHtml.replace(/{title}/g, title).replace(/{type}/g, confirmBoxHtml.replace(/{msg}/g, msg));
        $("body").append(html);

        //确认时间绑定
        $("#boxOk").bind("click", function () {
            if (ok != null) {
                ok();
            }
            $(".box_bg").remove();
        });

        //取消事件绑定
        $("#boxCancel").bind("click", function () {
            if (cancel != null) {
                cancel();
            }
            $(".box_bg").remove();
        });
    }

}



//回车事件
$(document).keydown(function (event) {
    if (event.keyCode == 13) { //绑定回车

        var isFocus = $("#box_input").is(":focus");
        if (true == isFocus) {
            $("#boxOk").click();

            //失去焦点
            $('#box_input').blur();
        }
        return false;
    }
});
